﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Pitaya_IDE
{
    public class FilePanel:Panel
    {
        private Label label_pic, label_file;
        string suffix;

        Form1 form1= null;

        public FilePanel(int FileDdepth,  Form1 _form1)
        {
            form1= _form1;

            Color backColor  = Color.FromArgb(1, 4, 9);

            //if (_Inode.Select)
            //{
            //    backColor = Color.FromArgb(45, 49, 57);
            //}

            //string[] sArray = _Inode.name.Split(new char[1] { '.' });

            //if (sArray.Length > 1)
            //{
            //    suffix = sArray[sArray.Length - 1];
            //}
            //else
            //{
            //    suffix = null;
            //}

            label_pic = new Label() //图案标签
            {
                Name = "label_pic",
                Size = new Size(20, 20),
                Location = new Point(FileDdepth * 12, 5),
                BackColor = Color.Transparent,
                Font = new Font("等线", 13),
                TextAlign = ContentAlignment.MiddleLeft,
                Cursor = Cursors.Hand
            };

            label_file = new Label() //文件名标签
            {
                Name = "label_file",
                AutoSize = true,
                Location = new Point(FileDdepth * 12 + 20, 6),
                BackColor = backColor,
                ForeColor = Color.FromArgb(201, 209, 217),
                //Text = FileInode.name,
                Font = new Font("等线", 13),
                TextAlign = ContentAlignment.MiddleLeft,
                Cursor = Cursors.Hand
            };

            this.BackColor = backColor;

            this.Controls.Add(label_pic);
            this.Controls.Add(label_file);

            this.MouseDown += new MouseEventHandler(File_MouseDown);
            this.MouseUp += new MouseEventHandler(File_MouseUP);
            this.MouseEnter += new EventHandler(File_MouseEnter);
            this.MouseLeave += new EventHandler(File_MouseLeave);

            label_pic.MouseDown += new MouseEventHandler(File_MouseDown);
            label_pic.MouseUp += new MouseEventHandler(File_MouseUP);
            label_pic.MouseEnter += new EventHandler(File_MouseEnter);
            label_pic.MouseLeave += new EventHandler(File_MouseLeave);

            label_file.MouseDown += new MouseEventHandler(File_MouseDown);
            label_file.MouseUp += new MouseEventHandler(File_MouseUP);
            label_file.MouseEnter += new EventHandler(File_MouseEnter);
            label_file.MouseLeave += new EventHandler(File_MouseLeave);

            //if (_Inode.Select)
            //{
            //    SetFileImage(45);
            //}
            //else
            //{
            //    SetFileImage(1);
            //}
            
        }


        private bool Mouseflag = false;
        private void File_MouseUP(object sender, EventArgs e)
        {
            if (Mouseflag)
            {
                //if (FileInode.IsFolder)
                //{ 
                //    if (FileInode.unfold) FileInode.unfold = false;
                //    else FileInode.unfold = true;

                //    /*文件夹从折叠到展开*/
                //    if ((!FileInode.unfold) && FileInode.GetNet == false && Fsp != null)
                //    {
                //        Fsp.GetFolder(FileInode);/* 发送ls命令 */
                //    }
                //}

                //if (FileInode.IsFolder)  //如果是目录
                //{
                //    if (FileInode.unfold)//折叠
                //    {
                //        label_pic.Image = AllImage.folderFHBH;
                //        FileInode.DeleteMonitor();  //删除监控
                //    }
                //    else  //未折叠
                //    {
                //        label_pic.Image = AllImage.folderFLBH;
                //        FileInode.Observer();  //添加监控
                //    }

                //    Fsp.Draw_folder(form1.LeftColumnWidth);
                //}
                //else
                //{ //文件按下
                //    form1.mainPanel.AddTitleFile(FileInode);
                //}
            }
            Mouseflag = false;
        }

        private void File_MouseDown(object sender, EventArgs e)=>Mouseflag = true;
        
        private void File_MouseEnter(object sender, EventArgs e)
        {
            Color set_cloor ;

            //if (FileInode.Select)
            //{
                set_cloor = Color.FromArgb(45, 49, 57);
            //    SetFileImage(45);
            //}
            //else
            //{
            //    set_cloor = Color.FromArgb(22, 27, 34);
            //    SetFileImage(22);
            //}

            this.BackColor = set_cloor;
            label_file.BackColor = set_cloor;
        }

        private void File_MouseLeave(object sender, EventArgs e)
        {
            Color set_cloor ;
            
            Point panelPoint = this.PointToClient(Control.MousePosition); //鼠标相对于pane左上角的坐标

            if (panelPoint.X > 5 && panelPoint.X < this.Width - 5 &&
                panelPoint.Y > 5 && panelPoint.Y < this.Height - 5)
            {
                return;
            }

            //if (FileInode.Select)
            //{
            set_cloor = Color.FromArgb(45, 49, 57);
            SetFileImage(45);
            //}
            //else
            //{
            //    set_cloor = Color.FromArgb(1, 4, 9);
            //    SetFileImage(1);
            //}

            this.BackColor = set_cloor;
            label_file.BackColor = set_cloor;
        }

        public void SetBright(bool i)
        {
            if (i)
            {
                this.BackColor = Color.FromArgb(45, 49, 57);
                label_file.BackColor = Color.FromArgb(45, 49, 57);
                SetFileImage(45);
            }
            else
            {
                this.BackColor = Color.FromArgb(1, 4, 9);
                label_file.BackColor = Color.FromArgb(1, 4, 9);
                SetFileImage(1);
            }
        }

        private void SetFileImage(int bright)
        {
            
            //if (FileInode.IsFolder)  //如果是目录
            //{
            //    if (bright == 1)
            //    {
            //        label_pic.Image = AllImage.GetImage(FileInode.unfold, false);
            //    }
            //    else
            //    {
            //        label_pic.Image = AllImage.GetImage(FileInode.unfold, true);
            //    }
            //}
            //else  //非目录  根据后缀决定用什么图标
            //{
            //    if (suffix != null)
            //    {
            //        if (suffix.Equals("py") || suffix.Equals("PY"))
            //        {
            //            label_pic.Image = AllImage.GetImage("py", bright);
            //        }
            //        else if (suffix.Equals("c") || suffix.Equals("C") || suffix.Equals("h") || suffix.Equals("H"))
            //        {
            //            label_pic.Image = AllImage.GetImage("c", bright);
            //        }
            //        else if (suffix.Equals("sh") || suffix.Equals("SH"))
            //        {
            //            label_pic.Image = AllImage.GetImage("sh", bright);
            //        }
            //        else //无法识别的后缀
            //        {
            //            label_pic.Image = AllImage.GetImage("default", bright);
            //        }
            //    }
            //    else //无后缀
            //    {
            //        label_pic.Image = AllImage.GetImage("default", bright);
            //    }
            //}
        }


    }
}